package com.custom.boot.config.autoconfigure;

import com.custom.boot.config.HttpClient;
import com.custom.boot.config.HttpProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@EnableConfigurationProperties(HttpProperties.class)
public class HttpAutoConfiguration {

    @Autowired
    private HttpProperties httpProperties;

    // 在Spring上下文中创建一个对象
    @Bean("httpClient")
    @Profile("default")
    @ConditionalOnProperty(value = "http.enable", havingValue = "true",
            matchIfMissing = false)
    public HttpClient httpClientDefault() {
        HttpClient httpClient = new HttpClient();
        String url = httpProperties.getUrl();
        httpClient.setUrl(url);
        System.out.println("动态读取地址");
        return httpClient;
    }

    @Bean("httpClient")
    @Profile("fixed")
    @ConditionalOnProperty(value = "http.enable", havingValue = "true",
            matchIfMissing = false)
    public HttpClient httpClientFixed() {
        HttpClient httpClient = new HttpClient();
        httpClient.setUrl("https://sohu.com");
        System.out.println("使用固定地址");
        return httpClient;
    }
}



